// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd.service;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import net.opentsdb.core.TSDB;
import net.opentsdb.tsd.HttpQuery;
import net.opentsdb.tsd.controller.RemoveTagsController;
import net.opentsdb.utils.hbase.HbaseWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;

/**
 * 清理tagk/tagv请求
 */
public class RemoveTagsService extends RpcServiceImpl{

    private static final Logger LOG = LoggerFactory.getLogger(RemoveTagsService.class);

    private String metric;
    private final HbaseWrapper wrapper;

    public RemoveTagsService(TSDB tsdb, HttpQuery query){
        super(tsdb, query);
        wrapper = tsdb.getHbaseWrapper();
    }

    @Override
    public void doPost() {
        doDelete();
    }

    @Override
    public void doDelete() {
        RemoveTagsController controller = new RemoveTagsController(tsdb, metric);
        try{
            controller.handle();
            putResponse(true,"delete tsuid success");
        } catch (Exception e) {
            LOG.error("clear tags metric: "+metric+" error:",e);
            putErrorResponse(false, "clear tags metric: "+metric+" error");
        }
    }

    @Override
    public void checks() {
        Map<String,String> map = (Map<String,String>)params;

        if(!map.containsKey("metric")){
            LOG.info("error delete tsuid request");
            throw new IllegalArgumentException("error type request, ex:{metric:'m1',tagk:'t1,t2'}");
        }

        metric = String.valueOf(((Map<String,String>)params).get("metric"));

        if(Strings.isNullOrEmpty(metric)){
            LOG.error("metric {} is null，params context：{} ", metric,
                    Joiner.on(" ").join(((Map<String, String>) params).entrySet()));
            throw new IllegalArgumentException("error type request, ex:{metric:'m1',tagk:'t1,t2'}");
        }

    }
}
